// 创建文件夹
import {ISqlInsert} from "../../global";
import MysqlServer from "../utils/connect";

export const fileAddDao = async (params: Array<string>): Promise<ISqlInsert> => {
    return await MysqlServer.sqlAsync<ISqlInsert, Array<string>>("INSERT INTO t_file(`fileUID`, `fileName`, `time`, `type`, `parentFileUID`) VALUES(?, ?, ?, ?, ?)", params)
}

export const fileUserAddDao = async (params: Array<string>): Promise<ISqlInsert> => {
    return await MysqlServer.sqlAsync<ISqlInsert, Array<string>>("INSERT INTO t_user_file(`userFileUID`, `userUID`, `fileUID`, `state`) VALUES(?, ?, ?, ?)", params)
}

export interface IFile {
    userFileUID: string
    userUID: string
    state: string
    fileName: string
    fileUID: string
    parentFileUID: string
    path: string
    size: number
    time: string
    type: string
}
export const fileListDao = async (params: Array<string>): Promise<Array<IFile>> => {
    const sql = `SELECT
            \tuf.userFileUID AS userFileUID,
            \tuf.userUID AS userUID,
            \tuf.state AS state,
            \tf.fileName AS fileName,
            \tf.fileUID AS fileUID,
            \tf.parentFileUID AS parentFileUID,
            \tf.path AS path,
            \tf.size AS size,
            \tf.time AS time,
            \tf.type AS type
            FROM t_user_file AS uf LEFT JOIN t_file AS f ON uf.fileUID = f.fileUID
            WHERE uf.userUID = ? AND f.parentFileUID = ?
            ORDER BY f.time DESC`
    return await MysqlServer.sqlAsync<Array<IFile>, Array<string>>(sql, params)
}

export const fileItemAddDao = async (params: [string, string, string, string, string, number, string]): Promise<ISqlInsert> => {
    return await MysqlServer.sqlAsync<ISqlInsert, [string, string, string, string, string, number, string]>("INSERT INTO t_file(`fileUID`, `fileName`, `time`, `type`, `parentFileUID`, `size`, `path`) VALUES(?, ?, ?, ?, ?, ?, ?)", params)
}

// 删除
export const fileDelDao = async (params: Array<string>): Promise<ISqlInsert> => {
    const fileUIDs = params.map(() => '?').join(',');
    return await MysqlServer.sqlAsync<ISqlInsert, Array<string>>(`DELETE FROM t_file WHERE fileUID IN (${fileUIDs})`, params)
}

// 删除文件关联的用户UID
export const fileUserDelDao = async (params: Array<string>): Promise<ISqlInsert> => {
    const fileUIDs = params.map(() => '?').join(',');
    return await MysqlServer.sqlAsync<ISqlInsert, Array<string>>(`DELETE FROM t_user_file WHERE fileUID IN (${fileUIDs})`, params)
}

// 根据文件UID获取文件path
export const filePathListDao = async (params: Array<string>): Promise<Array<{ path: string }>> => {
    const fileUIDs = params.map(() => '?').join(',');
    return await MysqlServer.sqlAsync<Array<{ path: string }>, Array<string>>(`SELECT path FROM t_file WHERE fileUID IN (${fileUIDs})`, params)
}